package com.jzlin.funhub.admin.service.chatai.support;

import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.core.thread.ThreadUtil;
import com.jzlin.funhub.admin.service.chatai.MockChatAiService;
import com.jzlin.funhub.admin.service.chatai.ZhipuChatAiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class ChatExecutor {
    ThreadPoolExecutor executor;

    @Autowired
    ZhipuChatAiService zhipuChatAiService;

    @Autowired
    MockChatAiService mockChatAiService;

    @PostConstruct
    public void init() {
        executor = ExecutorBuilder.create()
                .setCorePoolSize(5)
                .setMaxPoolSize(5)
                .setKeepAliveTime(60, TimeUnit.SECONDS)
                .setWorkQueue(new LinkedBlockingQueue<>(50)).build();
    }

    /**
     * 提交消息
     * @param message
     */
    public void executeMessage(BaseChatMessage message) {
        executor.execute(()-> {
            ThreadUtil.sleep(200);
            //mockChatAiService.invokeSse(message);
            zhipuChatAiService.handleMessage(message);
        });
    }
}
